#include "wifi_device.h"
#include "wifi_adapter.h"
#include "string.h"
#include "los_task.h"
#include "los_compiler.h"
#include "cmsis_os2.h"
#include "ohos_init.h"
#include "ohos_types.h"

WifiEvent g_testWifiEvent;

/** Connection state change */
static void OnWifiConnectionChanged(int state, WifiLinkedInfo *info)
{
    if(state == WIFI_STATE_NOT_AVAILABLE)
    {
        printf("Wi-Fi DisConnect\n");
    }
    else
    {
        //printf("Wi-Fi Connect to %s\n", info->ssid);
    }
}
/** Scan state change */
static void OnWifiScanStateChanged(int state, int size)
{
    (WifiScanInfo *)malloc(sizeof(WifiScanInfo)*size);
    if(state == WIFI_STATE_AVAILABLE)
    {
        printf("Wi-Fi Scan OK, get %d Hotspot\n", size);
        // 申请内存
        WifiScanInfo *info = (WifiScanInfo *)malloc(sizeof(WifiScanInfo)*size);
        int isize;
        // 获取扫描队列
        GetScanInfoList(info, &isize);
        if(info != NULL)
        {
            for(int i=0; i<size; i++)
            {
                // 打印扫描信息
                PrintScanInfo(&info[i]);
            }
            // 释放内存
            free(info);
        }    
    }
    else
    {
        printf("Wi-Fi Scan Failed\n");
    }
}
/** Hotspot state change */
static void OnHotspotStateChanged(int state)
{
    if(state == WIFI_STATE_AVAILABLE)
    {
        printf("Hotspot Enable\n");
    }
    else
    {
        printf("Hotspot Disable\n");
    }
}
/** Station connected */
static void OnHotspotStaJoin(StationInfo *info)
{
    char mac[18] = {0};
    char ip[16] = {0};
    mac_hex2str(mac, info->macAddress);
    int_to_ip(ip, info->ipAddress);
    printf("%s Join! IP is %s\n", mac, ip);
}
/** Station disconnected */
static void OnHotspotStaLeave(StationInfo *info)
{
    char mac[18] = {0};
    char ip[16] = {0};
    mac_hex2str(mac, info->macAddress);
    int_to_ip(ip, info->ipAddress);
    printf("%s Leave! IP is %s\n", mac, ip);
}

void wifi_entry()
{
    printf("----------Wi-Fi Test Start----------\n");
    WifiErrorCode res;
    if(WifiUartInit() != WIFI_SUCCESS)
    {
        printf("%s line %d error : Init wifi error...\n", __func__, __LINE__);
        return;
    }
    printf("----------Wi-Fi Test Start 1----------\n");
    g_testWifiEvent.OnHotspotStaJoin = OnHotspotStaJoin;
    g_testWifiEvent.OnHotspotStaLeave = OnHotspotStaLeave;
    g_testWifiEvent.OnHotspotStateChanged = OnHotspotStateChanged;
    g_testWifiEvent.OnWifiConnectionChanged = OnWifiConnectionChanged;
    g_testWifiEvent.OnWifiScanStateChanged = OnWifiScanStateChanged;
    RegisterWifiEvent(&g_testWifiEvent);
    if(EnableWifi() != WIFI_SUCCESS)
    {
        return;
    }
    printf("----------Wi-Fi Test Start 2----------\n");
    delay_ms(3000);
    Scan();
    WifiDeviceConfig config;
    memset(config.ssid, 0, WIFI_MAX_SSID_LEN);
    memset(config.preSharedKey, 0, WIFI_MAX_KEY_LEN);
    memcpy(config.ssid, WIFI_SSID, strlen(WIFI_SSID));
    memcpy(config.preSharedKey, WIFI_PASSWD, strlen(WIFI_PASSWD));
    int net_id;
    if(AddDeviceConfig(&config, &net_id) != WIFI_SUCCESS)
    {
        printf("%s line %d rror : Add Wi-Fi Device config error...\n", __func__, __LINE__);
        return;
    }
    delay_ms(3000);
    ConnectTo(net_id);
    printf("----------Wi-Fi Test Over----------\n");
}

void wifi_task()
{
    UINT32 uwRet = -1;
    UINT32 taskID = 0;
    TSK_INIT_PARAM_S stTask = {0};
    stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)wifi_entry;
    stTask.uwStackSize = 0x1000;
    stTask.pcName = "wifi task";
    stTask.usTaskPrio = 10;
    uwRet = LOS_TaskCreate(&taskID, &stTask);
    if (uwRet != LOS_OK) {
        printf("Task fs create failed\n");
    }
}